require([], function() {
    window.mainApp = {}  // 给iframe（子页面）调用的接口
    var tab = null;
    var accordion = null;
    var tree = null;
    var tabItems = [];
    var navTabCompleted = false,
        isShowSideNav = false,
        lastSelected2TabID = new Array(2), //保存最后两次选中的Nav的tabid
        $sideNav = $('.sideNav'),
        headNnav_H = $('.l-topmenu').height() + $('.mainNav').height();

    $(function() {

        //Tab
        $("#framecenter").ligerTab({
            height: 'auto',
            showSwitchInTab: true,
            showSwitch: false,
            changeHeightOnResize : true,
            isFixedFirstTab: true,       //针对Shiqiao租赁系统Home Tab 固定
            minIframeHeight: $(window).height() - headNnav_H -5,
            onAfterAddTabItem: function(tabdata) {
                tabItems.push(tabdata);
                saveTabStatus();
                $(window).resize();
            },
            onBeforeRemoveTabItem : function (tabid) {
                if (tab.getSelectedTabItemID() == tabid) {
                    tab.selectTabItem(lastSelected2TabID[0]);
                };
            },
            onAfterRemoveTabItem: function(tabid) {
                for (var i = 0; i < tabItems.length; i++) {
                    var o = tabItems[i];
                    if (o.tabid == tabid) {
                        tabItems.splice(i, 1);
                        saveTabStatus();
                        changeSideNav (tab.getSelectedTabItemID());
                        setTabContent ();
                        break;
                    }
                }
            },
            onAfterSelectTabItem : function (tabid) {
                if (navTabCompleted) {
                    changeSideNav (tabid);
                    setTabContent ();
                };
                lastSelected2TabID.shift();
                lastSelected2TabID.push(tabid);
            }
        });

        //面板
        $("#accordion1").ligerAccordion({
            height: "auto",
            speed: 200,
            opened: false,
            onAfterRenderAccordion: function () {
                setNanoScroller ();
            },
            onAfterToggleAccordion: function () {
                setNanoScroller ();
            }
        });

        tab = liger.get("framecenter");
        accordion = liger.get("accordion1");
        $("#pageloading").hide();

        pages_init();

        $('.homeBk_Btn').click(function() {
            tab.selectTabItem('home');
            tab.moveToTabItem('home');
        });
        $('#accordion1').on('click', '.l-link', function(event) {
            if ($(this).attr('data-method') == 'f_addTab')
            {
                event.preventDefault();
                var tabid=$(this).attr('tabid'),
                    text =$(this).attr('title'),
                    url  =$(this).attr('data-url');
                f_addTab(tabid, text, url);
                changeSideNav (tabid);
            };
        });
        if (isShowSideNav) {
            showSideNav ();
        }else{
            closeSideNav ();
        };
        $("#sideToggle").click(function() {
            if (!isShowSideNav) {
                showSideNav ();
                $('#layout1').addClass('showSideNav');
            } else {
                closeSideNav ();
                $('#layout1').removeClass('showSideNav');
            };
            isShowSideNav = !isShowSideNav;
        });

        var resizeTimer = null;
        $(window).resize(function(event) {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout(function () {
                tab.onResize();
                setSideNavHeight ();
                setNanoScroller ();
            }, 200);
        });
    });


    function showSideNav () {
            $sideNav.css({
                'visibility': 'visible',
                'top': headNnav_H
            }).show();
            setTabContent ()
    }
    function closeSideNav () {
            $sideNav.css('visibility', 'hidden').hide();
            setTabContent ()
    }
    function setTabContent () {
        if ($('.sideNav:visible').length) {
                $('.l-tab-content').css({
                    'padding-left': $sideNav.width() + parseInt($sideNav.css('left'))
                });
            } else{
                $('.l-tab-content').css({
                    'padding-left': 0
                });
            };
        tab.onResize();
    }

    function f_addTab(tabid, text, url) {
        tab.addTabItem({
            tabid: tabid,
            text: text,
            url: url
        });
    }
    // window.f_addTab = f_addTab;

    function pages_init() {
        var tabJson = $.cookie('liger-home-tab');
        if (tabJson) {
            var tabitems = JSON2.parse(tabJson);
            for (var i = 0; tabitems && tabitems[i]; i++) {
                f_addTab(tabitems[i].tabid, tabitems[i].text, tabitems[i].url);
            }
            if (tabitems.length > 0) {
                var lastTabID = tabitems[tabitems.length-1].tabid;
                changeSideNav (lastTabID);
            };
            navTabCompleted = true;
        };
    }

    function setNanoScroller () {
        setSideNavHeight ();
        $(".sideNav .nano").nanoScroller({
            preventPageScrolling: true
        });
    }
    function setSideNavHeight () {
        var newSideNavHeight = $(window).height() - headNnav_H;
        $('.sideNav').height(newSideNavHeight);
    }
    function changeSideNav (tabid) {
        var $selectedSideNav = $('#accordion1 [tabid='+tabid+']');
        $('#accordion1 .l-link').removeClass('l-link-selected');
        if ($selectedSideNav.length) {
            $selectedSideNav.addClass('l-link-selected');
            if ($selectedSideNav.parent(':visible').length == 0) {
                $selectedSideNav.parent().prev().click();
            };
        }else{
            $('.l-accordion-toggle-open-head').click();
        };
    }
    function saveTabStatus() {
        $.cookie('liger-home-tab', JSON2.stringify(tabItems));
    }
    function getQueryString(name) {
        var now_url = document.location.search.slice(1),
            q_array = now_url.split('&');
        for (var i = 0; i < q_array.length; i++) {
            var v_array = q_array[i].split('=');
            if (v_array[0] == name) {
                return v_array[1];
            }
        }
        return false;
    }

    $.extend(true, window.mainApp, {
        parentTab : tab,
        setTabContent : setTabContent,
        f_addTab : f_addTab
    });

});